//
//  localizControllerViewController.m
//  QRMaster
//  多语言选择
//  Created by murphybeyond on 15/8/19.
//  Copyright (c) 2015年 uclbrt. All rights reserved.
//

#import "localizControllerViewController.h"
#import "InternationalControl.h"

@interface localizControllerViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * languageTabelView;
    NSArray *languageArray;
    NSInteger languageIndex;
    NSInteger languageConstant;
    NSString * currentLanagage;
    
    UIButton * saveBtn;//保存
}

@end

@implementation localizControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLanguage) name:@"changeLanguage"object:nil];
    currentLanagage = [InternationalControl userLanguage];
    languageArray = @[@"中文简体",@"中文繁體",@"English"];
    if ([currentLanagage isEqualToString:@"zh-Hans"])
    {
        languageIndex = 0;
        languageConstant = 0;
    }
    else if([currentLanagage isEqualToString:@"zh-Hant-TW"])
    {
        languageIndex = 1;
        languageConstant = 1;
    }
    else if([currentLanagage isEqualToString:@"en"])
    {
        languageIndex = 2;
        languageConstant = 2;
    }
    [self prepareTitleView];
    [self prepareTableView];
    self.view.backgroundColor = RGBA(237, 238, 241, 1);
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    saveBtn.hidden = NO;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationItem.title = NSLocalizedString(@"lauguage", nil);
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    saveBtn.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//保证发送通知时至少有一个观察者
-(void)changeLanguage
{
    
}

-(void)prepareTitleView
{
    saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(UI_SCREEN_WIDTH - 56, 12, 40, 20);
    [saveBtn setTitle:NSLocalizedString(@"save", nil) forState:0];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [saveBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [saveBtn addTarget:self action:@selector(saveLanguage) forControlEvents:UIControlEventTouchUpInside];
    saveBtn.enabled = NO;
    [self.navigationController.navigationBar addSubview:saveBtn];
}

-(void)prepareTableView
{
    languageTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    languageTabelView.dataSource = self;
    languageTabelView.delegate = self;
    [self.view addSubview:languageTabelView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return languageArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.textLabel.text = languageArray[indexPath.row];
    if (indexPath.row == languageIndex)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
        cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
  
    languageIndex = indexPath.row;
    [languageTabelView reloadData];
    
    [self changeSaveBtnStatus];
    
}

#pragma mark - 判断save按钮是否可点击
- (void)changeSaveBtnStatus{
    if (languageIndex==languageConstant) {
        saveBtn.enabled = NO;
    }else{
        saveBtn.enabled = YES;
    }
}

-(void)saveLanguage
{
    NSString * lan = nil;
    switch (languageIndex) {
        case 0:
            lan = @"zh-Hans-CN";
            break;
        case 1:
            lan = @"zh-Hant-TW";
            break;
        case 2:
            lan = @"en";
            break;
        default:
            break;
    }
    [InternationalControl setUserlanguage:lan];
//    [QRSingleton sharedQRSingleton].currentLanguageIndex = languageIndex;
    //改变完成之后发送通知，告诉其他页面修改完成，提示刷新界面
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeLanguage" object:nil];
    [self languageWithCountry];
    [self BackClick];
}


#pragma mark ------------- 国家地区的多语言
-(void)languageWithCountry
{
    [QRSingleton sharedQRSingleton].currentAreaCodeString = [QRSingleton sharedQRSingleton].currentAreaCodeString;
}

-(void)BackClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"changeLanguage" object:nil];
}

@end
